在变量索引处插入字符串 [英] Inserting a string at a variable index

查看:78
本文介绍了在变量索引处插入字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法找到一个有效的例子,也没有一个很好的解释如何实现以下目标:(如果有人能指出我正确的方向,我将不胜感激。

I have not been able to find a working example or a good explanation of how I can achieve the following: (I would appreciate if anyone can point me in the right direction.

我有一个查询字符串: **/ api / bla?sources = [1,2]& plans = [1,2]& codes = [1, 2,3]**

I have a query string: **"/api/bla?sources=[1,2]&plans=[1,2]&codes=[1,2,3]"**

我将通过 javascript 更新查询字符串或 jquery 当我的页面上发生某些事件时,无关紧要。

I will be updating the query string via either javascript or jquery when certain events occur on my page, doesnt matter which.

例如,页面上有一个多选下拉菜单[来源]和[计划]和[代码] ...这些下拉列表中包含ID,我将通过选择更新我的请求网址下拉列表中的项目。

For example, there is a multi select dropdown on the page which houses [sources] and [plans] and [codes]... These dropdowns have IDs which i am to update my request url with upons selecting items in teh dropdowns.

当从下拉列表中选择ID为3的源时(或复选框,无论使用哪个页面控件)查询字符串参数sources [1,2]将需要附加3。同样,如果ID为2的项目为un选中后,它同样会从查询字符串中删除,将新字符串保留为来源[1,3]

When a source with ID "3" is selected from the dropdown (or checkbox, doesnt matter what page controls are being used) the query string parameter sources[1,2] will need a "3" appended. Likewise then if the item with an ID of "2" is unselected, it will likewise be removed from the query string leaving the new string as sources[1,3]

我对javascript有点新鲜/ jquery,尤其是更高级的字符串操作。我一直试图重新创建一些东西来证明这一点,并且已经完成了以下工作。

I am somewhat new to javascript/jquery and especially more advanced string manipulation. I have been attempting to recreate something to demonstrate this and have gotten to the following which is not fully working.

基本上我的初始if语句按预期工作,但是当下其他被点击(当需要将另一个ID添加到查询字符串中的现有模型时 - 比如[来源]或[代码]的第二个ID)它会返回wonky输出 - 因为我无法获得正确更新所有内容的正确公式。

Basically my initial if statement works as intended, but the moment the else is hit (when another ID needs to be added to an existing model in the query string - like a second ID for [sources] or [codes]) it returns wonky output - seeng as I couldnt get the right formula to update everything correctly.

//TIMEMTABLE QUERY
function updateCalendar(filter_id, filter_element) {
    //Run through the filter checks before making final call to query and update timetable?

    //GET THE MODEL/OBJECT NAME
    var queryName = filter_element.attr('data-owner');


    //GET THE IDs //this is either an array of all selected IDs or a single id which is used in the else statement
    var queryId = filter_element.attr('value');
    var queryIds = $('#'+filter_id).val();


    var modelCheckIndex = requestString.toLowerCase().indexOf(queryName.toLowerCase());
    //build a request string
    if (modelCheckIndex < 0) {
        console.info('ADD MODEL TO QUERY STRING');
        requestString = requestString + "&" + (queryName.toLowerCase() + "[" + queryIds + "]");
        console.log(requestString);
    }
    else{
        console.info('UPDATE MODEL ON QUERY STRING');
        var position = requestString.toLowerCase().indexOf(queryName.toLowerCase());
        //requestString = requestString.substr(modelCheckIndex -1, requestString.length -1) + "," + queryId + "]";
        requestString = requestString.slice(modelCheckIndex.indexOf("]"), modelCheckIndex) + "," + queryId;
        console.log(requestString);

    }

//MAKE THE API CALL USING CREATED QUERY STRING

}

如果有人有任何例子或小提琴,我也会很感激。
小提琴我正在尝试上班

If anyone has any examples or fiddles lying around I would also appreciate it. Fiddle I am trying to get to work

推荐答案

我建议稍微改变一下逻辑。我会为所需参数使用一些数据存储,并在每次必要时重建请求字符串,如下例所示。

I suggest to change the logic a bit. I would use some data storage for the wanted parameter and rebuild the request string every time when it's necessary, like the below example.

它比重建更好更好每当某个值发生变化时都会串起来。

It is much more better, than rebuild the string each time when some value has changed.

var data = {
        sources: [1, 2],
        plans: [],
        codes: [1, 3, 4]
    };

function buildStr() {
    function get(key) { return key + '=' + JSON.stringify(data[key]); }
    return '/api/bla?' + ['sources', 'plans', 'codes'].map(get).join('&');
}

document.write('<pre>' + buildStr() + '</pre>');

这篇关于在变量索引处插入字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆