jquery tabledit 下拉列表:是否可以从 mysql 数据库中检索值? [英] jquery tabledit dropdown: is it possible to retrieve values from mysql database?
问题描述
我正在使用 jquery tabledit 插件来更新数据库.与官方示例中的工作完全一样.
I'm using the jquery tabledit plug-in to update a database. Works perfectly like in the official examples.
我可以成功地包含具有固定数量选项(在 custom_table_edit.js 中定义)的静态下拉列表.
I can succesfuly include a static dropdown with a fixed number of options (defined in custom_table_edit.js).
我希望能够从数据库中动态获取这些选项,但我不知道如何自定义 custom_table_edit.js 中的代码.
I'd like to be able to dynamically get those options from a database instead, but I don't know how to customize the code in custom_table_edit.js.
我可以在 php 中用一个循环查询数据库并生成一个 html 字段来编码.但我不了解 javascript 或者在这个框架中它是否可能.
I can code this in php with a loop querying the database and generating a html <select>
field. But I don't have knowledge of javascript or if it's even possible in this framework.
这是 custom_table_edit.js 文件.下拉列表定义了三种颜色选项.我希望动态生成此下拉列表.
This is the custom_table_edit.js file. A dropdown is defined with three colour options. I want this dropdown to be dynamically produced.
// custom_table_edit.js
$('#example2').Tabledit({
url: 'example.php',
eventType: 'dblclick',
editButton: false,
columns: {
identifier: [0, 'id'],
editable: [[1, 'car'], [2, 'color', '{"1": "Red", "2": "Green", "3": "Blue"}']]
}
});
我真的没有尝试过任何东西,因为我想知道是否可以在这个框架中做.
I really haven't tried anything because i'd like to know if it's possible to do in this framework.
推荐答案
欢迎来到 SO,nuclear.
Welcome to SO, nucelar.
您所描述的是客户端通过 JavaScript 向服务器发出的 HTTP 请求.这通常称为 AJAX 或异步 JavaScript 和XML.此 API 可让您手动向服务器发送请求,并且有多种实现方式.
What you are describing is a HTTP request from the client to server through JavaScript. This is commonly referred as AJAX or Asynchronous JavaScript And XML. This API enables you to manually send requests to the server and there are multiple implementations.
因为您使用的是 jQuery,我建议您使用 $.ajax 函数它包含在 jQuery 库中.
Because you are using jQuery I will recommend you to use the $.ajax function which is included in the jQuery library.
在这里,我做了一个非常基本的示例,说明如何使用 GET
方法向服务器发送 HTTP 请求以检索一些数据.
Down here I've made a very basic example of how to send a HTTP request to a server with the GET
method to retrieve some data.
$.ajax({
url: 'https://yourdomain.com', // Where to send the request to. Can also be a file.
method: 'GET', // What method of request it uses.
success: function(data) { // When a response is succesfully received.
// Do something with the received data.
console.log(data); // Show what the data looks like in the console.
},
error: function(jqXHR, textStatus, errorThrown) { // When an error occurs while making a request.
console.log(jqXHR, textStatus, errorThrown); // Show the error in the console.
}
});
在您的情况下,url
属性值可能是您在其中查询数据库并返回结果的 PHP 文件的 URL,正如您提到的那样.
In your case the url
property value might be the URL of a PHP file in which you query the database and return the result, as you mentioned you are able to do.
AJAX 函数的响应(存储在 success
方法中的 data
变量中)可以是文本,如 string
,如果您想发送结构化数据,甚至可以使用 JSON.
The response of the AJAX function (which is stored in the data
variable in the success
method) can be text, as in a string
, or even JSON if you want to send structured data.
注意异步部分.这意味着 AJAX 代码不会停止其余 JavaScript 代码的执行,而只是在 HTTP 请求完成时继续执行并返回.
Beware of the Asynchronous part. This means that the AJAX code does not stop the execution of the rest of your JavaScript code, but simply continues and comes back whenever the HTTP request has been completed.
我希望这足以让您入门.祝你好运,不要犹豫,提出问题.
I hope that this is enough to get you started. Good luck and don't hesitate to ask questions.
这篇关于jquery tabledit 下拉列表:是否可以从 mysql 数据库中检索值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!